package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.agent.builder.LambdaFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 登录控制器
 */
@RestController
@RequestMapping("/mobile/login")
@Slf4j
@Api(tags = "手机端登录接口")
public class MobileLoginController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private MemberService memberService;

    @PostMapping("/smsLogin")
    //由于前端给的手机号和验证码没有实体类进行接受,我们可以自己写一个也可以用Map来代替实体类接受
    public Result smsLogin(@RequestBody Map map, HttpSession session){
        log.info("用户开始登录");
        String phone = map.get("telephone").toString();
        log.info("telephone{}",phone);
        String code = map.get("validateCode").toString();
        log.info("telephone{}",code);
        //从Redis中拿到这个验证码
        String key =RedisConst.VALIDATE_CODE_PREFIX+"MOBILE_SIGNIN_"+phone;
        Object redisCode = redisTemplate.opsForValue().get(key);
        log.info("从redis中拿到了验证码+{}",redisCode);
        //拿到前端的数据后可以判断手机号是否存在,验证码是否正确
        if(redisCode!=null && redisCode.equals(code)) {
            //如果拿到了验证码且与输入的验证码一样则redis中验证码失去作用,删除
            redisTemplate.delete(key);
            //如果此用户没有注册过则自动注册
//            User user = userService.selectPhone(phone);//根据手机号查询用户信息
//            if (user==null){
//                User newUser = new User();
//                newUser.setTelephone(phone);
//                newUser.setStation("1");
////                log.info(newUser.getTelephone(),newUser.getStation());
//                userService.addPhone(newUser);
//            }
//            session.setAttribute("user",user.getId());//将用户信息存储到session中,“user”键,user.getId值


            //去数据库查询该手机号是否是会员,如果是则修改会员信息;如果不是则添加为会员
            Member member = memberService.selectPhoneNumber(phone);//根据手机号查询会员信息
            if (member == null){
                Member newMember = new Member();
                newMember.setPhoneNumber(phone);
                memberService.addMember(newMember);
                log.info("新建会员成功");
            }
            Member phoneNumber = memberService.selectPhoneNumber(phone);
            session.setAttribute("phoneNumber",phoneNumber.getId());

            return new Result(true,"登录成功");
        }
        return new Result(false,"登录失败");
    }
}
